package leetcode.树;

import java.util.ArrayList;
import java.util.List;

/**
 * 107. 二叉树的层次遍历 II
 */
public class Test107二叉树的层次遍历II {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }


    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        recursive(root, ans, 0);
        return ans;
    }

    public void recursive(TreeNode node, List<List<Integer>> ans, int level) {
        if (node == null) {
            return;
        }

        if (level == ans.size()) {
            ans.add(0, new ArrayList<>());
        }
        ans.get(ans.size() - level - 1).add(node.val);
        recursive(node.left, ans, level+1);
        recursive(node.right, ans, level+1);

    }
}
